package com.zt.frame;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * SysIntecepter
 * 
 * @author zhangzhw
 * @describe 系统拦截器
 */
public class SysIntecepter extends HandlerInterceptorAdapter {

	/**
	 * 方法执行前拦截
	 */
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
			Object handler) {
		Logger logger = Logger.getLogger(handler.getClass());

		String className = handler.getClass().getName();// packageName.ClassName

		if (className.equals("com.zt.sysman.LoginController")) {
			return true;
		}

		// SUser sUser = (SUser) req.getSession().getAttribute("user");

		// if (null == sUser) {
		// try {
		// req.getRequestDispatcher("").forward(paramServletRequest,
		// paramServletResponse);
		// RequestDispatcher rd=req.getRequestDispatcher("")
		// resp.sendRedirect("/sys/login/login");
		// String cp = req.getContextPath(); // /tydxweb
		// req.getRequestURI(); // /tydxweb/sys/menu/menuList.xml
		// req.getRequestURL(); //
		// http://localhost:8080/tydxweb/sys/menu/menuList.xml
		// String uri = req.getRequestURI();
		//
		// String type = uri.substring(uri.indexOf("."), uri.length());
		// // req.getContentType(); //null
		// try {
		// req.getRequestDispatcher("/sys/login/notLogin" + type).forward(req,
		// resp);
		// return false; // true 则走下边的拦截器， false 则不走拦截器
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

		// return true;
		// } catch (IOException ioe) {
		// return false;
		// }
		// }

		logger.info("准备执行请求" + className + "中的方法");

		// if (Error) {
		// return false;
		// }
		return true;
	}

	/**
	 * 方法执行后,返回视图前拦截
	 */
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		Logger logger = Logger.getLogger(handler.getClass());
		String className = handler.getClass().getName();// packageName.ClassName
		logger.info("已执行请求" + className+ "中的方法");
	}

	/**
	 * 执行完成拦截
	 */
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		Logger logger = Logger.getLogger(handler.getClass());
		String className = handler.getClass().getName();// packageName.ClassName
		logger.info("请求" + className + "执行完成");
	}

}
